﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using MobileInterviewer.interview;

namespace MobileInterviewer
{
    public partial class MainForm : Form
    {
        private int cur;
        private Question question;
        private Interview interview;
        private ArrayList componentList;
        private string[] template;

        public MainForm()
        {
            InitializeComponent();
            componentList = new ArrayList();
            template = loadTemplate("test");
            interview = Deserialize(template[1]);
            cur = 0;
            question = (Question)interview.questions[cur];
            showQuestion(question);
        }

        private string[] loadTemplate(string name)
        {
            pl.studentlive.nosoifighters.DataProviderService ws = new MobileInterviewer.pl.studentlive.nosoifighters.DataProviderService();
            return ws.GetFormTemplateByName(name);
        }

        private String UTF8ByteArrayToString(Byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(characters,0,characters.Length-1);
            return (constructedString);
        }

        private Byte[] StringToUTF8ByteArray(String pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        public String Serialize(Interview interview)
        {
            try
            {
                String XmlizedString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(Interview));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, interview);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
                return XmlizedString;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                return null;
            }
        }

        public Interview Deserialize(String pXmlizedString)
        {
            XmlSerializer xs = new XmlSerializer(typeof(Interview));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (Interview) xs.Deserialize(memoryStream);
        }

        public void showQuestion(Question q)
        {
            Label label = new Label();
            label.Name = "questionLabel";
            label.Text = q.label;
            label.Location = new System.Drawing.Point(3, 3);
            label.Size = new System.Drawing.Size(160, 40);
            this.Controls.Add(label);
            componentList.Add(label);

            if (q.type.Equals("input"))
            {
                TextBox textBox = new TextBox();
                textBox.Name = "questionTextBox";
                textBox.Location = new System.Drawing.Point(3, 50);
                textBox.Size = new System.Drawing.Size(160, 40);
                this.Controls.Add(textBox);
                componentList.Add(textBox);
            }
            else if (q.type.Equals("check"))
            {
                int i = 0;
                foreach (var a in q.answers)
                {
                    CheckBox checkbox = new CheckBox();
                    checkbox.Text = (string)a;
                    checkbox.Name = checkbox.Text;
                    checkbox.Location = new System.Drawing.Point(3, 40 + i * 30);
                    checkbox.Size = new System.Drawing.Size(160, 30);
                    this.Controls.Add(checkbox);
                    componentList.Add(checkbox);
                    i++;
                }
            }
            else if (q.type.Equals("radio"))
            {
                int i = 0;
                foreach (var a in q.answers)
                {
                    RadioButton radioButton = new RadioButton();
                    radioButton.Text = (string)a;
                    radioButton.Name = radioButton.Text;
                    radioButton.Location = new System.Drawing.Point(3, 40 + i * 30);
                    radioButton.Size = new System.Drawing.Size(160, 30);
                    this.Controls.Add(radioButton);
                    componentList.Add(radioButton);
                    i++;
                }
            }
        }

        private void clearForm()
        {
            foreach (var i in componentList)
            {
                this.Controls.Remove((Control)i);
            }
            componentList.Clear();
        }

        private void nextMenuItem_Click(object sender, EventArgs e)
        {
            saveData();
            if (cur < interview.questions.Count - 1)
            {
                if (cur == 0)
                {
                    backMenuItem.Text = "Previous";
                }
                cur++;
                clearForm();
                question = (Question)interview.questions[cur];
                showQuestion(question);
                if (cur == interview.questions.Count - 1)
                {
                    nextMenuItem.Text = "Send";
                }
            }
            else
            {
                sendForm(interview);
                //Program.toHelloForm();
                Program.setState(Program.helloFormState);
            }
        }

        private void sendForm(Interview interview)
        {
            pl.studentlive.nosoifighters.DataProviderService ws = new MobileInterviewer.pl.studentlive.nosoifighters.DataProviderService();
            //localhost.DataProviderService ws = new localhost.DataProviderService();
            DateTime time = DateTime.Now;
            ws.SetForm("interview "+time, Serialize(interview), int.Parse(template[0]));
            MessageBox.Show("interview data sent to server");
        }

        private void saveData()
        {
            if (question.type.Equals("input"))
            {
                foreach (var component in componentList)
                {
                    if (component is TextBox)
                    {
                        question.userAnswer = ((TextBox)component).Text;
                    }
                }
            }
            else if (question.type.Equals("radio"))
            {
                foreach (var component in componentList)
                {
                    if (component is RadioButton)
                    {
                        RadioButton rb = (RadioButton)component;
                        if (rb.Checked)
                        {
                            question.userAnswer = rb.Text;
                        }
                    }
                }
            }
            else if (question.type.Equals("check"))
            {
                foreach (var component in componentList)
                {
                    if (component is CheckBox)
                    {
                        CheckBox cb = (CheckBox)component;
                        if (cb.Checked)
                        {
                            if (question.userAnswer.Equals(""))
                            {
                                question.userAnswer = cb.Text;
                            }
                            else
                            {
                                question.userAnswer = question.userAnswer + ", " + cb.Text;
                            }
                        }
                    }
                }
            }
        }

        private void backMenuItem_Click(object sender, EventArgs e)
        {
            saveData();
            if (cur > 0)
            {
                if (cur == interview.questions.Count - 1)
                {
                    nextMenuItem.Text = "Next";
                }
                cur--;
                clearForm();
                question = (Question)interview.questions[cur];
                showQuestion(question);
                if (cur > 0)
                {
                    backMenuItem.Text = "Back";
                }
            }
            else
            {
                Program.toHelloForm();
            }
        }
    }
}